﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NUnit.Framework;

namespace InSTEDD.Labs.Geo.Tests
{
	[TestFixture]
	public class LatLongFixture
	{
		[Test]
		public void ShouldEqualLatLongInstances()
		{
			LatLong latLong1 = new LatLong(11.22, 33.44);
			LatLong latLong2 = new LatLong(11.22, 33.44);
			LatLong latLong3 = new LatLong(11.55, 33.66);

			Assert.AreEqual(latLong1, latLong2);
			Assert.AreNotEqual(latLong1, latLong3);
			Assert.AreNotEqual(latLong1, new object());
		}

		[Test]
		[ExpectedException(typeof(ArgumentOutOfRangeException))]
		public void ShouldThrowIfLatLongOutOfRange()
		{
			// invalid Lat
			LatLong latLong1 = new LatLong(-100, 20);

			// invalid Long
			LatLong latLong2 = new LatLong(50, -200);

			// invalid Lat & Long
			LatLong latLong3 = new LatLong(500, 600);
		}

		[Test]
		public void ShouldParseLatLongDecimalStrings()
		{
			LatLong latLong1 = LatLong.Parse("50", "-20");
			LatLong latLong2 = new LatLong(50, -20);
			Assert.AreEqual(latLong1, latLong2);

			LatLong latLong3 = LatLong.Parse("-80 170");
			LatLong latLong4 = new LatLong(-80, 170);
			Assert.AreEqual(latLong3, latLong4);
		}

		[Test]
		public void ShouldParseWithComasInsteadOfDots()
		{
			LatLong latLong = LatLong.Parse("11,22 33,44");

			Assert.IsNotNull(latLong);
			Assert.AreEqual(11.22d, latLong.Lat);
			Assert.AreEqual(33.44d, latLong.Long);
		}
	
		[Test]
		public void ShouldParseSingleStringWithWhitespaces()
		{
			LatLong latLong1 = LatLong.Parse(" 50    -20");
			LatLong latLong2 = new LatLong(50, -20);
			Assert.AreEqual(latLong1, latLong2);
		}

		[Test]
		public void ShouldParseSingleStringWithAlternateSeparator()
		{
			LatLong latLong1 = LatLong.Parse("50*-20", '*');
			LatLong latLong2 = new LatLong(50, -20);
			Assert.AreEqual(latLong1, latLong2);
		}

		[Test]
		public void ShouldParseSingleStringWithAlternateSeparatorAndWhitespaces()
		{
			LatLong latLong1 = LatLong.Parse(" 50   *  -20", '*');
			LatLong latLong2 = new LatLong(50, -20);
			Assert.AreEqual(latLong1, latLong2);
		}

		[Test]
		public void ShouldParseDMSString()
		{
			LatLong latLong = LatLong.Parse("37.28.39.79 122.9.2.13");

			Assert.IsNotNull(latLong);
			Assert.AreEqual(37.477719, latLong.Lat);
			Assert.AreEqual(122.150592, latLong.Long);
		}

		[Test]
		public void ShouldParseNegativeDMSString()
		{
			LatLong latLong = LatLong.Parse("-37.28.39.79 -122.9.2.13");

			Assert.IsNotNull(latLong);
			Assert.AreEqual(-37.477719, latLong.Lat);
			Assert.AreEqual(-122.150592, latLong.Long);
		}

		[Test]
		public void ShouldParseDMSStringWithoutMinutes()
		{
			LatLong latLong = LatLong.Parse("37.28.39 122.9.2");

			Assert.IsNotNull(latLong);
			Assert.AreEqual(37.4775, latLong.Lat);
			Assert.AreEqual(122.150556, latLong.Long);
		}
	}
}
